#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>

int main(){
	int pipefd[2];
	int ret=pipe(pipefd);
	ssize_t byte=0;
	char buf[256];
	if(ret==-1){
		perror("pipe");
		exit(1);
	}
	ret=fork();
	if(ret==-1){
		perror("fork");
		exit(1);
	}
	if(ret==0){
		close(pipefd[1]);
		byte=read(pipefd[0],buf,sizeof(buf));
		if(byte==-1){
			perror("read");
			close(pipefd[0]);
			exit(1);
		}
		write(0,buf,strlen(buf));
		close(pipefd[0]);
		exit(0);
	}else{
		
		strcpy(buf,"hello word!\n");
		close(pipefd[0]);
		byte=write(pipefd[1],buf,strlen(buf));
		if(byte==-1){
			perror("write");
			close(pipefd[1]);
			exit(1);
		}
		close(pipefd[1]);
		while((waitpid(-1,NULL,WNOHANG))==0);
		while((waitpid(-1,NULL,WNOHANG))==0);
	}
	return 0;
}
